package ciicos.service.impl;

import ciicos.base.ActionResult;
import ciicos.entity.*;
import ciicos.mapper.LinePlaceMapper;
import ciicos.model.visiual.superQuery.SuperQueryJsonModel;
import ciicos.service.*;
import ciicos.util.relation.SubRealtion;
import ciicos.util.relation.WrapperRelation;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.util.ObjectUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.permission.service.AuthorizeService;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;
import ciicos.model.lineplace.*;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ciicos.util.*;

import java.util.*;

/**
 * 位置管理
 * 版本： V3.2.0
 * 版权：
 * 作者： 企业研发中心
 * 日期： 2025-10-03
 */
@Service
public class LinePlaceServiceImpl extends ServiceImpl<LinePlaceMapper, LinePlaceEntity> implements LinePlaceService {

    @Autowired
    private LinePlaceImagesService linePlaceImagesService;

    @Autowired
    private LineLinePlaceRelationService lineLinePlaceRelationService;

    private final static Class eClass = LinePlaceEntity.class;

    @Override
    public List<LinePlaceEntity> getList(LinePlacePagination pagination) {
        String dataType = pagination.getDataType();
        QueryWrapper<LinePlaceEntity> queryWrapper = new QueryWrapper<>();
        WrapperRelation wrapperRelation = new WrapperRelation(eClass);
        wrapperRelation.getSubRealtionList().add(new SubRealtion(linePlaceImagesService, LinePlaceImagesEntity.class, "id", "placeid"));
        AutoCodeUtil.highQuery(pagination.getSuperQueryJson(), queryWrapper, wrapperRelation);


        //查询条件
        if (ObjectUtil.isNotEmpty(pagination.getCode())) {
            queryWrapper.lambda().like(LinePlaceEntity::getCode, pagination.getCode());
        }

        if (ObjectUtil.isNotEmpty(pagination.getName())) {
            queryWrapper.lambda().like(LinePlaceEntity::getName, pagination.getName());
        }

        if (ObjectUtil.isNotEmpty(pagination.getType())) {
                queryWrapper.lambda().eq(LinePlaceEntity::getType, pagination.getType());
            if(StringUtil.equals(pagination.getType(),"1") && ObjectUtil.isNotEmpty(pagination.getWellType())){
                List<String> wellTypeList = ListUtil.parsefromString(pagination.getWellType());
                if(ListUtil.isNotEmpty(wellTypeList)) {
                    queryWrapper.lambda().in(LinePlaceEntity::getType, wellTypeList);
                }
            }
        }

        if (ObjectUtil.isNotEmpty(pagination.getSelectKey())) {
            queryWrapper.lambda()
                    .like(LinePlaceEntity::getCode, pagination.getSelectKey())
                    .or()
                    .like(LinePlaceEntity::getName, pagination.getSelectKey());
        }

        //排序+分页
        return AutoCodeUtil.sortPageList(this, queryWrapper, eClass,
                pagination.getSidx(), pagination.getSort(),
                LinePlaceEntity::getId, true, pagination, StringUtil.equals("0", dataType));
    }

    @Override
    public LinePlaceEntity getInfo(String id) {
        QueryWrapper<LinePlaceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LinePlaceEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(LinePlaceEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, LinePlaceEntity entity) {
        return this.updateById(entity);
    }

    @Override
    public void delete(LinePlaceEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    @Override
    public List<LinePlaceImagesEntity> getLinePlaceImagesList(String linePlaceId) {
        QueryWrapper<LinePlaceImagesEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LinePlaceImagesEntity::getPlaceid, linePlaceId);
        return linePlaceImagesService.list(queryWrapper);
    }

    @Override
    public List<LinePlaceImagesEntity> getLinePlaceImagesMapList(List<String> linePlaceIdList) {
        QueryWrapper<LinePlaceImagesEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(LinePlaceImagesEntity::getPlaceid, linePlaceIdList);
        return linePlaceImagesService.list(queryWrapper);
    }

    //验证表单
    @Override
    public ActionResult checkForm(LinePlaceForm form, String id) {
        boolean isUp = StringUtil.isNotEmpty(form.getId()) && !form.getId().equals("0");
        if ("0".equals(id)) {//创建
            if (isUp) {
                return ActionResult.fail("创建时不能传id");
            }
        } else {
            if (!StringUtil.equals(id, form.getId())) {
                return ActionResult.fail("更新id校验错误");
            }
        }

        if (ObjectUtil.isEmpty(form.getName())) {
            return ActionResult.fail("名称不能为空");
        }

        if (ObjectUtil.isEmpty(form.getCode())) {
            form.setCode(form.getName());
        }
        QueryWrapper<LinePlaceEntity> queryWrapper = new QueryWrapper<>();
        if (isUp) {
            queryWrapper.lambda().ne(LinePlaceEntity::getId, id);
        }
        queryWrapper.lambda().and(t ->
                t.eq(LinePlaceEntity::getCode, form.getCode()).or().eq(LinePlaceEntity::getName, form.getName())
        );

        LinePlaceEntity entity = this.getOne(queryWrapper);
        if (entity == null) {
            return ActionResult.success();
        } else {
            if (StringUtil.equals(form.getName(), entity.getName())) {
                return ActionResult.fail("名称不能重复");
            } else {
                return ActionResult.fail("编码不能重复");
            }
        }


    }

    @Override
    public long getTypeCount() {
        QueryWrapper<LinePlaceEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct F_TYPE");
        return this.getBaseMapper().selectCount(queryWrapper);
    }

    @Override
    public List<LineLineEntity> getLineListByPlaceId(String placeId) {
        return lineLinePlaceRelationService.getLineListByPlaceId(placeId);
    }


}
